<%@page import="com.smfx.yxt.utils.DateUtil"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
	String currDate = DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>新增荣誉-云校通-教师</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<body ontouchstart>
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui-header bg-blue"> 
				<div class="weui-header-left"> <a href="<%=basePath %>/ls/student/groupup/${student.id}?to=award" class="icon icon-109 f-white">返回</a></div>
				<h1 class="weui-header-title">新增荣誉</h1>
			</div>
	        <div class="weui_cells"style="margin: 0;">
	        	<div class="weui_cell">
					<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/cansaixuesheng@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">获奖学生:</p>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<p>${student.name }</p>
					</div>
				</div>
	        	<div class="weui_cell" style="align-items: stretch;">
	        		<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/jingsaimingcheng@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
	        		<div class="weui_cell_hd">
						<p class="pr15">竞赛名称:<p>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="competitionName" name="competitionName" class="weui_input" placeholder="请输入竞赛名称"/>
					</div>
				</div>
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/zuopinmingcheng@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">作品名称:</p>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="worksName" name="worksName" class="weui_input" placeholder="请输入作品名称"/>
					</div>
				</div>
			</div>
			<div class="weui_cells">
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/huojiangdengji@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">获奖级别:</p>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<select class="weui_select" id="awardGrade" name="awardGrade">
	                        <option value="">请选择</option>
	                    </select>
					</div>
				</div>
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/huojiangdengji@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">获奖等级:</p>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<select class="weui_select" id="awardLevel" name="awardLevel">
	                        <option value="">请选择</option>
	                    </select>
					</div>
				</div>
	            <div class="weui_cell">
	            	<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/huojiangshijian@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">获奖时间:</p>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input type="date" id="awardDate" name="awardDate" class="weui_input" value="<%=currDate %>"/>
					</div>
				</div>
	            <div class="weui_cell">
	            	<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/shoujiangdanwei@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">授奖单位:</p>
					</div>
	                <div class="weui_cell_bd weui_cell_primary">
                		<input id="awardCompany" name="awardCompany" class="weui_input" placeholder="请输入授奖单位"/>
            		</div>
	            </div>
	        	<div class="weui_cell">
					<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/zhidaojiaoshi@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">指导老师:</p>
					</div>
	                <div class="weui_cell_bd weui_cell_primary">
                		<input id="guideTeacher" name="guideTeacher" class="weui_input" placeholder="请输入指导老师"/>
            		</div>
	            </div>
			</div>
	        <div class="weui_cells">
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/huojiangzhaopian@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15">获奖照片</p>
					</div>
					<div class="camera">
						<img alt="" src="<%=basePath %>/images/shenpi_xiangji@3x.png">
						<input id="imgid" class="dispear" type="file">
					</div>
				</div>
				<div class="weui_cell">
					<ul class="img-click" id="img1" style="list-style-type: none;margin: 0 5px 5px 0;">
					</ul>
				</div>
			</div>
	       <div class="weui_btn_area">
		        <a href="javascript:" id="btnSubmit" class="weui_btn bg-blue">确认提交</a>
		    </div>
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script>
	$(function() {
		initAwardType();
		$("#btnSubmit").click(function() {
			submit();
		});
	})
	function initAwardType() {
		$.ajax({
			url : basePath + "/ls/student/groupup/teaFruParam",
			type : "post",
			dataType : "json",
			async : false,
			success : function(data) {
				if(data != null) {
					var gradeHtml = "";
					var levelHtml = "";
					var grade = data["获奖级别"];
					var level = data["获奖等第"];
					var gradeArray = grade.split(",");
					for(var i in gradeArray) {
						gradeHtml += '<option value="'+gradeArray[i]+'">'+gradeArray[i]+'</option>';
					}
					$("#awardGrade").append(gradeHtml);
					var levelArray = level.split(",");
					for(var i in levelArray) {
						levelHtml += '<option value="'+levelArray[i]+'">'+levelArray[i]+'</option>';
					}
					$("#awardLevel").append(levelHtml);
				}
			}, error:function(){
			}
		});
	}
	function submit() {
		var competitionName = $("#competitionName").val();
		if(competitionName.length == 0) {
			$.alert("请输入竞赛名称!","");
			return;
		}
		var worksName = $("#worksName").val();
		if(worksName.length == 0) {
			$.alert("请输入学生作品名称!","");
			return;
		}
		var awardGrade = $("#awardGrade").val();
		if(awardGrade.length == 0) {
			$.alert("请选择获奖级别!","");
			return;
		}
		var awardLevel = $("#awardLevel").val();
		if(awardLevel.length == 0) {
			$.alert("请选择获奖等级!","");
			return;
		}
		var awardDate = $("#awardDate").val();
		if(awardDate.length == 0) {
			$.alert("请选择获奖时间!","");
			return;
		}
		var awardCompany = $("#awardCompany").val();
		if(awardCompany.length == 0) {
			$.alert("请输入授奖单位!","");
			return;
		}
		var guideTeacher = $("#guideTeacher").val();
		if(guideTeacher.length == 0) {
			$.alert("请输入指导老师!","");
			return;
		}
		var attachIds = "";
		var imgList = $("#img1").find("li");
		if(imgList.length > 0) {
			imgList.each(function(i,v){
				var id = $(v).attr("data-id");
				attachIds += id;
				if((i+1) < imgList.length) {
					attachIds += ",";
				}
			});
		}
		$.ajax({
			url: basePath + '/ls/student/groupup/studentFruit/submit',
			type: 'post',
			dataType: 'json',
			data: {
				competitionName: competitionName,
				worksName: worksName,
				awardGrade: awardGrade,
				awardLevel: awardLevel,
				awardDate: awardDate,
				awardCompany: awardCompany,
				guideTeacher: guideTeacher,
				studentId: "${student.id}",
				attachIds: attachIds
			},
			success: function(data) {
				if (data.code == '000') {
					$.toast("提交成功！");
					window.location.href = basePath + "/ls/student/groupup/${student.id}?to=award";
				} else {
					$.toptips(data.msg);
				}
			},
            error: function(xhr, type){
            	$.toptips("系统异常");
            }
		});
	}
	
	document.querySelector('#imgid').onchange = function(e) {
		var files = e.target.files;
		var len = files.length;
		for (var i=0; i < len; i++) {
			lrz(files[i], {
				width:1080
			})
			.then(function (rst) {
				var xhr = new XMLHttpRequest();
	            xhr.open('POST', basePath + "/ls/attchment/upload/lrz");
	            xhr.onload = function () {
	                if (xhr.status === 200) {
	                	var obj = JSON.parse(xhr.response)
	                	$("#img1").append('<li class="weui_uploader_file" data-id="'+obj.data.id+'" style="margin-right:0;margin-bottom:10px;position: relative;"><div class="img-del" onclick="imgDel(this)"><span class="icon icon-26"></span></div><img onclick="imgClick(this)" src="'+ rst.base64 +'"/></li>')
	                }
	            };
	            xhr.onerror = function () {
	            	$.toptips("上传异常");
	            };
	            xhr.upload.onprogress = function (e) {
	                // 上传进度
	                var percentComplete = ((e.loaded / e.total) || 0) * 100;
	            };
	            // 添加参数
	            rst.formData.append('attachmentName', rst.origin.name);
	            rst.formData.append('attachmentSize', rst.fileLen);
	            rst.formData.append('baseData', rst.base64);
	            rst.formData.append('attachmentType', "image");
	            rst.formData.append('relationType', "studentFruits");
	            // 触发上传
	            xhr.send(rst.formData);
	            return rst;
			})
	        .always(function () {
	            // 不管是成功失败，都会执行
	        });
			;
		}
	}

	//拍照或从手机相册中选图接口
	$('#filePicker').on('click', function () {
		wx.chooseImage({
		    count: 9,
		    needResult: 1,
		    sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
		    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
		    success: function (res) {
		   		var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
		   		wxuploadImage(localIds);
		    },
		    fail: function (res) {
		        alterShowMessage("操作提示", JSON.stringify(res), "1", "确定", "", "", "");
		    }
		
		});
	});
	function wxuploadImage(localIds) {  
		var localId = localIds.pop();
		wx.uploadImage({  
		    localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得  
		    isShowProgressTips: 1, // 默认为1，显示进度提示  
		    success: function (res) {  
		        mediaId = res.serverId; // 返回图片的服务器端ID
		        wechatMediaDownload(mediaId);
		        if(localIds.length > 0){
		        	wxuploadImage(localIds);
		        }
		    },  
		    fail: function (error) {  
		        alert(Json.stringify(error));  
		    }  
		});
	}  
	function wechatMediaDownload(mediaId) {
		var params = {};
		params.mediaId = mediaId;
		params.attachmentType = "image";
		params.relationType = "studentFruits";
		$.ajax({
			url : basePath + "/ls/attchment/upload/wx",
			type : "post",
			data : params,
			dataType : "json",
			async : false,
			success : function(data) {
				if(data.code != '000') {
					$.toast(data.msg, "cancel");
				}else {
					$('#img1').append('<li class="weui_uploader_file" data-id="'+data.data.id+'" style="margin-right:0;margin-bottom:16px;"><div class="img-del" onclick="imgDel(this)"><span class="icon icon-26"></span></div><img onclick="imgClick(this)" src="'+ imgUrl + data.data.attachmentUrl +'"/></li>'); 
				}
			}, error:function(){
				$.toptips("上传异常");
				}
		});
	}
	
</script>
</html>